﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace AGameOfThrones.network
{
    public class client
    {
        public connection Connection { get; set; }

        public ui.manager UIManager
        {
            get
            {
                try
                {
                    return System.Windows.Application.Current.FindResource("uimanager") as ui.manager;
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex);
                    return null;
                }

            }
        }  
        
        public void CloseConnection()
        {
            if (Connection != null)
            {
                Connection.Abort();
            }
        }


        public void OpenConnection(String IP, int Port)
        {
            IPAddress ipAddr = IPAddress.Parse(IP);
            TcpClient tcpClient = new TcpClient();
            
            try
            {
                tcpClient.Connect(ipAddr, Port);
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.ToString());
                return;
            }

            Connection = new connection(tcpClient);
            Connection.OnNetworkConnected += new connection.OnNetworkConnectedDelegate(Connection_OnNetworkConnected);
            Connection.OnNetworkDisConnected += new connection.OnNetworkDisConnectedDelegate(Connection_OnNetworkDisConnected);
            Connection.OnNetworkData += new connection.OnNetworkDataDelegate(Connection_OnNetworkData);
            Connection.Start();
        }

        void Connection_OnNetworkData(object sender, connection.OnNetworkDataEvent e)
        {
            if (e.Message.GetType() == typeof(messages.process_movepiece))
            {
                if (UIManager != null)
                {
                    messages.process_movepiece p = e.Message as messages.process_movepiece;
                    if (!UIManager.Dispatcher.CheckAccess())
                    {
                         UIManager.Dispatcher.Invoke(
                           System.Windows.Threading.DispatcherPriority.Normal
                           , new ui.manager.OnMovePieceDelegate(UIManager.MovePiece)
                           , new ui.manager.MovePieceEvent(p)
                           );
                    }
                }
            }
        }

        void Connection_OnNetworkDisConnected(object sender, connection.OnNetworkDisConnectedEvent e)
        {
        }

        void Connection_OnNetworkConnected(object sender, connection.OnNetworkConnectedEvent e)
        {
        }

        public void SendMessage(messages.message message)
        {
            if (Connection != null)
            {
                Connection.SendMessage(message);
            }
        }
    }
}
